const mysql = require('mysql');
let nodeEnv = process.env.NODE_ENV
if (nodeEnv) nodeEnv = nodeEnv.trim()
// 数据库信息
const dbInfo = {
    host: 'localhost',
    user: 'root',
    password: 'liszter123',
    port: '3306',
    database: 'smart',
    // debug: true, //
    multipleStatements: true, // 一次支持多条语句
}
// 创建数据池
const pool = mysql.createPool(dbInfo);
let db = function (sql, values) {
    console.log('-----------', sql)
    return new Promise((resolve, reject) => {
        pool.getConnection(function (err, connection) {
            if (err) {
                reject(err)
            } else {
                connection.query(sql, values, (err, rows) => {
                    if (err) {
                        reject(err)
                    } else {
                        resolve(rows)
                    }
                    // 结束会话
                    connection.release()
                })
            }
        })
    })
}

module.exports = db